package com.sx.yqxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sx.yqxt.pojo.bo.ExtData;
import com.sx.yqxt.pojo.bo.MTRBo;
import com.sx.yqxt.pojo.bo.Total;
import com.sx.yqxt.pojo.entity.Record;
import com.sx.yqxt.mapper.RecordMapper;
import com.sx.yqxt.pojo.vo.*;
import com.sx.yqxt.service.RecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sx.yqxt.utils.HttpResquest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-06-13
 */
@Service
@EnableAsync
@Transactional
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Autowired
    private RecordMapper recordMapper;
    private static Map map = (Map) HttpResquest.Get("https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=317238855885").get("data");
    private static Map map2 = (Map) HttpResquest.Get("https://gwpre.sina.cn/ncp/foreign?_=1585388721640").get("result");

    @Async
    @Scheduled(fixedDelay = 1000*60*60*24)
    @Transactional
    public void updateA(){
        System.out.println("定时更新");
        map = (Map) HttpResquest.Get("https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=317238855885").get("data");
        recordMapper.delete(null);
        List<MTRBo> areaTree = getList((List<Map>) map.get("areaTree"),0);
        List<MTRBo> chinaDayList = getList((List<Map>) map.get("chinaDayList"),1);
        String date = (String) map.get("lastUpdateTime");
        MTRBo chinaTotal = getMTRBo2((Map) map.get("chinaTotal"),null,2);
        QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>();
        recordQueryWrapper.eq("type",2);
        Record record = recordMapper.selectOne(recordQueryWrapper);
        record.setDate(date);
        recordMapper.updateById(record);
    }

    @Override
    public Map updateMes() {
        recordMapper.delete(null);
        List<MTRBo> areaTree = getList((List<Map>) map.get("areaTree"),0);
        List<MTRBo> chinaDayList = getList((List<Map>) map.get("chinaDayList"),1);
        String date = (String) map.get("lastUpdateTime");
        MTRBo chinaTotal = getMTRBo2((Map) map.get("chinaTotal"),null,2);
        QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>();
        recordQueryWrapper.eq("type",2);
        Record record = recordMapper.selectOne(recordQueryWrapper);
        record.setDate(date);
        recordMapper.updateById(record);
        return map;
    }

    @Override
    public RecordVO getRecordVo() {
        map= (Map) HttpResquest.Get("https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=317238855885").get("data");
        MTRBo chinaTotal = getMTRBo((Map) map.get("chinaTotal"),null,2);
        chinaTotal.setDate((String) map.get("lastUpdateTime"));
        return new RecordVO(chinaTotal);
    }
    @Override
    public RecordVO2 getRecordVo2() {
        map2= (Map) HttpResquest.Get("https://gwpre.sina.cn/ncp/foreign?_=1585388721640").get("result");
        Map total = (Map) map2.get("total");
        RecordVO2 worldMap = new RecordVO2();
        worldMap.setCon(Integer.parseInt((String) total.get("certain")));
        worldMap.setAdd_con((String) total.get("certain_inc"));
        worldMap.setRecover(Integer.parseInt((String) total.get("recure")));
        worldMap.setAdd_recover((String) total.get("recure_inc"));
        worldMap.setDead(Integer.parseInt((String) total.get("die")));
        worldMap.setAdd_dead((String) total.get("die_inc"));
        worldMap.setOut_now_con(worldMap.getCon()-worldMap.getRecover()-worldMap.getDead());
        Integer a = Integer.parseInt(worldMap.getAdd_con().substring(1, worldMap.getAdd_con().length()));
        Integer b = Integer.parseInt(worldMap.getAdd_dead().substring(1,worldMap.getAdd_dead().length()));
        Integer c = Integer.parseInt(worldMap.getAdd_recover().substring(1,worldMap.getAdd_recover().length()));
        Integer d = a-b-c;
        worldMap.setAdd_out_now_con(d>0?"+"+d:d.toString());
        worldMap.setLastUpdateTime((String) map.get("lastUpdateTime"));

        return worldMap;
    }


    @Override//累计
    public List<MapVO> getMap1() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 0).ne("pid", 0));
        List<MapVO> mapVOS = new ArrayList<>();
        records.forEach(record -> {
            mapVOS.add(new MapVO(record.getName(),record.getConfirm()));
        });
        return mapVOS;
    }

    @Override
    public List<MapVO> getMap2() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 0).ne("pid", 0));
        List<MapVO> mapVOS = new ArrayList<>();
        records.forEach(record -> {
            mapVOS.add(new MapVO(record.getName(),record.getConfirm()-record.getDead()-record.getHeal()));
        });
        return mapVOS;
    }

    @Override//累计
    public List<MapVO> getMap3() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 0).eq("pid", 0));
        List<MapVO> mapVOS = new ArrayList<>();
        records.forEach(record -> {
            mapVOS.add(new MapVO(record.getName(),record.getConfirm()));
        });
        return mapVOS;
    }

    @Override
    public List<MapVO> getMap4() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 0).eq("pid", 0));
        List<MapVO> mapVOS = new ArrayList<>();
        records.forEach(record -> {
            mapVOS.add(new MapVO(record.getName(),record.getConfirm()-record.getDead()-record.getHeal()));
        });
        return mapVOS;
    }

    @Override
    public List<ChartNumVO1> getChar1() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 1));
        List<ChartNumVO1> chartNumVO1s = new ArrayList<>();
        records.forEach(s->{
            chartNumVO1s.add(new ChartNumVO1(s));
        });

        return chartNumVO1s;
    }
    @Override
    public List<ChartNumVO2> getChar2() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 1));
        List<ChartNumVO2> chartNumVO2s = new ArrayList<>();
        records.forEach(s->{
            chartNumVO2s.add(new ChartNumVO2(s));
        });
        return chartNumVO2s;
    }
    @Override
    public List<ChartNumVO3> getChar3() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().eq("type", 1));
        List<ChartNumVO3> chartNumVO3s = new ArrayList<>();
        records.forEach(s->{
            chartNumVO3s.add(new ChartNumVO3(s));
        });
        return chartNumVO3s;
    }

    @Override
    public List<TableVO> getTableVO() {
        List<Record> records = baseMapper.selectList(new QueryWrapper<Record>().ne("pid", 0).eq("type",0));
        List<TableVO> tableVOs = new ArrayList<>();
        records.forEach(r->{
            tableVOs.add(new TableVO(r));
        });
        return tableVOs;
    }


    private List<MTRBo> getList(List<Map> chinaDayList, Integer type){
        List<MTRBo> list = new ArrayList<>();

        for (Map m:chinaDayList){
            System.out.println(m);
            MTRBo mtrBo = getMTRBo2(m,null,type);
            list.add(mtrBo);
            if(m.get("name")!=null&&m.get("name").equals("中国")){
                List<Map> lt = (List<Map>) m.get("children");
                if (lt!=null&&!lt.isEmpty()){
                    for(Map map:lt){
                        getMTRBo2(map,mtrBo.getPid(),type);
                    }
                }
            }

        }
        return list;
    }

    private MTRBo getMTRBo(Map m, Long pid, Integer type){
        MTRBo mtrBo = new MTRBo();
        if (m.get("name")!=null)
            mtrBo.setName((String) m.get("name"));
        mtrBo.setDate((String) m.get("date"));
        Map total = (Map) m.get("total");
        Total tt = new Total();
        tt.setInput((Integer) total.get("input"));
        tt.setStoreConfirm((Integer) total.get("storeConfirm"));
        tt.setConfirm((Integer) total.get("confirm"));
        tt.setDead((Integer) total.get("dead"));
        tt.setHeal((Integer) total.get("heal"));
        tt.setSevere((Integer) total.get("severe"));
        tt.setSuspect((Integer) total.get("suspect"));
        mtrBo.setTotal(tt);
        tt = new Total();
        total = null;
        Map extData = (Map) m.get("extData");
        if(extData!=null){
            ExtData et = new ExtData();
            et.setIncrNoSymptom((Integer) extData.get("incrNoSymptom"));
            et.setNoSymptom((Integer) extData.get("noSymptom"));
            mtrBo.setExtData(et);
        }
        total = (Map) m.get("today");
        if (total!=null){
            tt.setInput((Integer) total.get("input"));
            tt.setStoreConfirm((Integer) total.get("storeConfirm"));
            tt.setConfirm((Integer) total.get("confirm"));
            tt.setDead((Integer) total.get("dead"));
            tt.setHeal((Integer) total.get("heal"));
            tt.setSevere((Integer) total.get("severe"));
            tt.setSuspect((Integer) total.get("suspect"));
            mtrBo.setToday(tt);
        }
        mtrBo.setPid(pid);
        Record record = new Record(mtrBo, type);
//        recordMapper.insert(record);
        mtrBo.setPid(record.getId());
//        mtrBo.setDate();
        return mtrBo;
    }
    private MTRBo getMTRBo2(Map m, Long pid, Integer type){
        MTRBo mtrBo = new MTRBo();
        if (m.get("name")!=null)
            mtrBo.setName((String) m.get("name"));
        mtrBo.setDate((String) m.get("date"));
        Map total = (Map) m.get("total");
        Total tt = new Total();
        tt.setInput((Integer) total.get("input"));
        tt.setStoreConfirm((Integer) total.get("storeConfirm"));
        tt.setConfirm((Integer) total.get("confirm"));
        tt.setDead((Integer) total.get("dead"));
        tt.setHeal((Integer) total.get("heal"));
        tt.setSevere((Integer) total.get("severe"));
        tt.setSuspect((Integer) total.get("suspect"));
        mtrBo.setTotal(tt);
        tt = new Total();
        total = null;
        Map extData = (Map) m.get("extData");
        if(extData!=null){
            ExtData et = new ExtData();
            et.setIncrNoSymptom((Integer) extData.get("incrNoSymptom"));
            et.setNoSymptom((Integer) extData.get("noSymptom"));
            mtrBo.setExtData(et);
        }
        total = (Map) m.get("today");
        if (total!=null){
            tt.setInput((Integer) total.get("input"));
            tt.setStoreConfirm((Integer) total.get("storeConfirm"));
            tt.setConfirm((Integer) total.get("confirm"));
            tt.setDead((Integer) total.get("dead"));
            tt.setHeal((Integer) total.get("heal"));
            tt.setSevere((Integer) total.get("severe"));
            tt.setSuspect((Integer) total.get("suspect"));
            mtrBo.setToday(tt);
        }
        mtrBo.setPid(pid);
        Record record = new Record(mtrBo, type);
        recordMapper.insert(record);

        if(m.get("name")!=null&&m.get("name").equals("中国")){
            mtrBo.setPid(record.getId());
        }
//        mtrBo.setDate();
        return mtrBo;
    }
}
